
package com.houseliker.activity.calculate;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.houseliker.Constants;
import com.houseliker.R;
import com.houseliker.fragment.BaseFragment;
import com.houseliker.fragment.calculate.GongJiJinFragment;
import com.houseliker.fragment.calculate.ShangYeFragment;
import com.houseliker.fragment.calculate.ZuHeFragment;
import com.houseliker.utils.StringUtils;
import com.houseliker.view.IViewPageIndicator;

public class PieChartFragment extends BaseFragment {
    private View mView;

    private TextView tv_gongjijin;
    private TextView tv_shangye;
    private TextView tv_zuhe;

    private IViewPageIndicator indicator;
    private ViewPager container;
    private SectionsPagerAdapter mSectionsPagerAdapter;

    public static double gjjLoanAmount;//公积金总价
    public static double syLoanAmount;//商业总价
    public static int gjjType;//公积金贷款类型：等额本息(0)/等额本金(1)
    public static int syType;//商业贷款类型：等额本息(0)/等额本金(1)
    public static int zhType;//组合贷款类型：等额本息(0)/等额本金(1)

    //公积金-贷款月利率
    public static double GJJ_RATE_1_5 = 0.0275 / 12;
    public static double GJJ_RATE_5 = 0.0325 / 12;
    //商业-贷款月利率
    public static double SY_RATE_1_3 = 0.0475 / 12;
    public static double SY_RATE_3_5 = 0.0475 / 12;
    public static double SY_RATE_5 = 0.049 / 12;

    //默认贷款月数240个月（20年）
    public static int GJJ_MONTHCOUNT = 20 * 12;
    public static int SY_MONTHCOUNT = 20 * 12;

    private GongJiJinFragment gjjFragment;
    private ShangYeFragment syFragment;
    private ZuHeFragment zhFragment;


    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragmentManager = getFragmentManager();
        transaction = fragmentManager.beginTransaction();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mView = setView(inflater, R.layout.my_piechart, TYPE_HEADER);

        initViews();

        registListener();

        fillDatas();

        return mView;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private void initViews() {
        tv_gongjijin = (TextView) mView.findViewById(R.id.tv_gongjijin);
        tv_shangye = (TextView) mView.findViewById(R.id.tv_shangye);
        tv_zuhe = (TextView) mView.findViewById(R.id.tv_zuhe);

        indicator = (IViewPageIndicator) mView.findViewById(R.id.indicator);
        container = (ViewPager) mView.findViewById(R.id.container);
    }

    private void registListener() {
        tv_gongjijin.setOnClickListener(onClicker);
        tv_shangye.setOnClickListener(onClicker);
        tv_zuhe.setOnClickListener(onClicker);
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    setHeaderBar("公积金贷款", "重置");
                    tv_gongjijin.setTextColor(getResources().getColor(R.color.greenAD));
                    tv_shangye.setTextColor(getResources().getColor(R.color.black3A));
                    tv_zuhe.setTextColor(getResources().getColor(R.color.black3A));
                    if (null == gjjFragment) {
                        gjjFragment = new GongJiJinFragment();
                    }
                    gjjFragment.fillDatas();
                } else if (position == 1) {
                    setHeaderBar("商业贷款", "重置");
                    tv_gongjijin.setTextColor(getResources().getColor(R.color.black3A));
                    tv_shangye.setTextColor(getResources().getColor(R.color.greenAD));
                    tv_zuhe.setTextColor(getResources().getColor(R.color.black3A));
                    if (null == syFragment) {
                        syFragment = new ShangYeFragment();
                    }
                    syFragment.fillDatas();
                } else {
                    setHeaderBar("组合贷款", "重置");
                    tv_gongjijin.setTextColor(getResources().getColor(R.color.black3A));
                    tv_shangye.setTextColor(getResources().getColor(R.color.black3A));
                    tv_zuhe.setTextColor(getResources().getColor(R.color.greenAD));
                    if (null == zhFragment) {
                        zhFragment = new ZuHeFragment();
                    }
                    zhFragment.fillDatas();
                }
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private View.OnClickListener onClicker = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            transaction = fragmentManager.beginTransaction();
            switch (v.getId()) {
                case R.id.tv_gongjijin:
                    indicator.setCurrentItem(0);
                    break;
                case R.id.tv_shangye:
                    indicator.setCurrentItem(1);
                    break;
                case R.id.tv_zuhe:
                    indicator.setCurrentItem(2);
                    break;
                default:
                    break;
            }
            transaction.commit();
        }
    };

    private void fillDatas() {
        setHeaderBar("公积金贷款", "重置");
        //读取利率
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(Constants.SP_RATE, Context.MODE_PRIVATE);
        String GJJ_RATE_1_5 = sharedPreferences.getString(Constants.KEY_GJJ_1_5, null);
        String GJJ_RATE_5 = sharedPreferences.getString(Constants.KEY_GJJ_5, null);
        String SY_RATE_1_3 = sharedPreferences.getString(Constants.KEY_SY_1_3, null);
        String SY_RATE_3_5 = sharedPreferences.getString(Constants.KEY_SY_3_5, null);
        String SY_RATE_5 = sharedPreferences.getString(Constants.KEY_SY_5, null);
        if (StringUtils.canParseDouble(GJJ_RATE_1_5)) {
            this.GJJ_RATE_1_5 = Double.parseDouble(GJJ_RATE_1_5) / 100d / 12d;
        }
        if (StringUtils.canParseDouble(GJJ_RATE_5)) {
            this.GJJ_RATE_5 = Double.parseDouble(GJJ_RATE_5) / 100d / 12d;
        }
        if (StringUtils.canParseDouble(SY_RATE_1_3)) {
            this.SY_RATE_1_3 = Double.parseDouble(SY_RATE_1_3) / 100d / 12d;
        }
        if (StringUtils.canParseDouble(SY_RATE_3_5)) {
            this.SY_RATE_3_5 = Double.parseDouble(SY_RATE_3_5) / 100d / 12d;
        }
        if (StringUtils.canParseDouble(SY_RATE_5)) {
            this.SY_RATE_5 = Double.parseDouble(SY_RATE_5) / 100d / 12d;
        }
        //静态变量需要重新赋值。
        //
        gjjLoanAmount = 0;
        syLoanAmount = 0;
        //
        gjjType = 0;
        syType = 0;
        zhType = 0;
        //贷款月数，
        GJJ_MONTHCOUNT = 20 * 12;
        SY_MONTHCOUNT = 20 * 12;

        //
        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

        // Set up the ViewPager with the sections adapter.
        container.setOffscreenPageLimit(0);
        container.setAdapter(mSectionsPagerAdapter);

        indicator.setViewPager(container);
        indicator.setSelectedColor(getResources().getColor(R.color.orange0F));
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            if (position == 0) {
                if (null == gjjFragment) {
                    gjjFragment = new GongJiJinFragment();
                }
                return gjjFragment;
            } else if (position == 1) {
                if (null == syFragment) {
                    syFragment = new ShangYeFragment();
                }
                return syFragment;
            } else {
                if (null == zhFragment) {
                    zhFragment = new ZuHeFragment();
                }
                return zhFragment;
            }
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "SECTION 1";
                case 1:
                    return "SECTION 2";
                case 2:
                    return "SECTION 3";
            }
            return null;
        }
    }

    @Override
    protected void onRightButtonClick() {
        super.onRightButtonClick();
        if (container.getCurrentItem() == 0) {
            gjjFragment.resetValues();
        } else if (container.getCurrentItem() == 1) {
            syFragment.resetValues();
        } else if (container.getCurrentItem() == 2) {
            zhFragment.resetValues();
        }
    }
}
